/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef UCOP_UTIL_LOCKEDEDQUEUE_H_
#define UCOP_UTIL_LOCKEDEDQUEUE_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <cassert>
#include <deque>
#include "Ucop/Macros.h"
#include "Ucop/System/LockGuard.h"
using Ucop::System::LockGuard;


namespace Ucop {
namespace Util {

    template< typename T, typename LockType, typename StorageType = std::deque<T> >
    class LockedQueue
    {
    public:
        LockedQueue(void)
            : _Mylock()
            , _Myqueue()
            , _Mycanceled(false)
        {
        }
        virtual ~LockedQueue(void)
        {
        }


        void Add(const T& _Item)
        {
            UCOP_TYPE_LOCK_GURAD_ASSERT(LockType, _Mylock);
            _Myqueue.push_back(_Item);
        }

        bool Next(T& _Result)
        {
            UCOP_TYPE_LOCK_GURAD_ASSERT(LockType, _Mylock);

            if (_Myqueue.empty()) {
                return false;
            }
            _Result = _Myqueue.front();
            _Myqueue.pop_front();
            return true;
        }

        template<class Checker>
        bool Next(T& _Result, Checker& _Check)
        {
            UCOP_TYPE_LOCK_GURAD_ASSERT(LockType, _Mylock);

            if (_Myqueue.empty()) {
                return false;
            }
            _Result = _Myqueue.front();
            if (!_Check.Process(_Result)) {
                return false;
            }
            _Myqueue.pop_front();
            return true;
        }

        T& Peek(void)
        {
            lock();
            T& _Result = _Myqueue.front();
            return _Result;
        }

        void Cancel(void)
        {
            UCOP_TYPE_LOCK_GURAD_ASSERT(LockType, _Mylock);
            _Mycanceled = true;
        }

        bool IsCancelled(void)
        {
            UCOP_TYPE_LOCK_GURAD_ASSERT(LockType, _Mylock);
            return _Mycanceled;
        }

        void Lock(void) { this->_Mylock.lock(); }
        void Unlock(void) { this->_Mylock.unlock(); }

        bool Empty(void)
        {
            UCOP_TYPE_LOCK_GURAD_ASSERT(LockType, _Mylock);
            return _Myqueue.empty();
        }

    private:
        LockType _Mylock;
        StorageType _Myqueue;
        volatile bool _Mycanceled;
    };

}}

#endif // UCOP_UTIL_LOCKEDEDQUEUE_H_
